Page({
  onShareAppMessage() {
    return {
      title: 'editor',
      path: 'page/component/pages/editor/editor'
    }
  },
  data: {
    imgList: [],
    navTabList: ['活动内容', '活动详情', '其他设置'],
    tabCur: 0,
    barginTime: [],  //砍价次数
    barginTimeValue: [0, 0],
    validTime: [],   //有效期
    validTimeValue: [0, 0],
    themeId:-1,
    // 富文本属性
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,
    safeHeight: 0,
    toolBarHeight: 50,
    activityContent: '',
  },
  // 完成
  saveActivityTap() {
    console.log(this.data.activityContent)
  },
  // 活动主题
  selectThemeTap(e){
    this.setData({
      themeId:e.currentTarget.dataset.id
    })
  },
  // 砍价次数次数设置
  barginTimeChange(e) {
    this.setData({
      barginTimeValue: e.detail.value
    })
  },
  // 富文本失去焦点
  editorBindblur(res) {
    this.setData({
      activityContent: res.detail.html
    })
  },
  readOnlyChange() {
    this.setData({
      readOnly: !this.data.readOnly
    })
  },
  onLoad() {
    // 富文本事件
    const { platform, safeArea, model, screenHeight } = wx.getSystemInfoSync()
    let safeHeight;
    if (safeArea) {
      safeHeight = (screenHeight - safeArea.bottom);
    } else {
      safeHeight = 32;
    }
    this._safeHeight = safeHeight;
    let isIOS = platform === 'ios'
    this.setData({ isIOS, safeHeight, toolBarHeight: isIOS ? safeHeight + 50 : 50 })
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      if (res.height === keyboardHeight) {
        return
      }
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            that.updatePosition(keyboardHeight)
            that.editorCtx.scrollIntoView()
          }
        })
      }, duration)
    })
    // 
    this.initData()
  },
  // 初始化砍价次数范围
  initData() {
    // 砍价次数
    var barginTime_arr = []
    for (var i = 1; i <= 100; i++) {
      barginTime_arr.push(i)
    }
    var barginTime = [barginTime_arr, barginTime_arr]
    // 砍价有效期
    var validTime_day = []
    var validTime_hour = []
    for (var i = 0; i <= 31; i++) {
      validTime_day.push(i + '日')
    }
    for (var i = 0; i <= 24; i++) {
      validTime_hour.push(i + '时')
    }
    var validTime = [validTime_day, validTime_hour]
    this.setData({
      barginTime: barginTime,
      validTime: validTime
    })
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 50
    const { windowHeight, platform } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    if (keyboardHeight === 0) {
      this.setData({
        editorHeight, keyboardHeight,
        toolBarHeight: this.data.isIOS ? 50 + this._safeHeight : 50,
        safeHeight: this._safeHeight,
      })
    } else {
      this.setData({
        editorHeight, keyboardHeight,
        toolBarHeight: 50,
        safeHeight: 0,
      })
    }
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const { statusBarHeight, platform } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },
  blur() {
    this.editorCtx.blur()
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({ formats })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        // 上传到服务器
        const tempFilePaths = res.tempFilePaths
        // 插入到富文本中
        that.editorCtx.insertImage({
          // 返回的地址路径
          src: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10006.jpg',
          data: {
            id: 'abcd',
            role: 'god'
          },
          width: '80%',
          success: function () {
            console.log(that.editorCtx)
          }
        })
      }
    })
  },
  // 导航条
  tabSelect(e) {
    this.setData({
      tabCur: e.currentTarget.dataset.id,
    })
  },
  lastStepTap() {
    let tabCur = this.data.tabCur
    if (tabCur > 0) {
      this.setData({
        tabCur: tabCur - 1
      })
    }
  },
  nextStepTap() {
    let tabCur = this.data.tabCur
    if (tabCur < this.data.navTabList.length - 1) {
      this.setData({
        tabCur: this.data.tabCur + 1
      })
    }
  }
})
